﻿using System;
using System.Data;
using System.Configuration; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for RegexValidator
/// </summary>
public class RegexValidator
{
    public RegexValidator()
    {

    }

    public static string StripTagsCharArray(string source)
    {
        char[] array = new char[source.Length];
        int arrayIndex = 0;
        bool inside = false;

        for (int i = 0; i < source.Length; i++)
        {
            char let = source[i];
            if (let == '<')
            {
                inside = true;
                continue;
            }
            if (let == '>')
            {
                inside = false;
                continue;
            }
            if (!inside)
            {
                array[arrayIndex] = let;
                arrayIndex++;
            }
        }
        return new string(array, 0, arrayIndex);
    }


    // Function To test for Alphabets
    public bool IsAlpha(String strToCheck)
    {
        Regex objAlphaPattern = new Regex("[^a-zA-Z]");
        return !objAlphaPattern.IsMatch(strToCheck);
    }

    // Function To test for AlphaNumeric
    public bool IsAlphaNumeric(String strAlphaNumeric)
    {
        Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9{2}]");
        return !objAlphaNumericPattern.IsMatch(strAlphaNumeric);
    }

    // Function to test for Positive Integers with zero inclusive
    public bool IsWholeNumber(String strNumber)
    {
        Regex objNotWholePattern = new Regex("[^0-9]");
        return !objNotWholePattern.IsMatch(strNumber);
    }

    public bool IsNaturalNumber(String strNumber)
    {
        Regex objNotWholePattern = new Regex("[^1-9]");
        return !objNotWholePattern.IsMatch(strNumber);
    }
    // Function to test for numeric with fixed length(6)
    public bool IsWholeNumberFixedLength_Pin(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex("^[0-9]{6}");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }
    // Function to test for numeric with fixed length(10)
    public bool IsWholeNumberFixedLength_Mobile(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex("^[0-9]{10}");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }

    // Function to test for numeric without length
    public bool IsWholeNumber_Phone(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex("^[0-9]");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }
    // Function to test for proper E-MailID format
    public bool IsRegularExpression_Email(String strmail)
    {
        Regex objmail = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        return objmail.IsMatch(strmail);
    }
    public bool IsValidatePwdFixedLength(String strpwd)
    {
        Regex objmail = new Regex(@"^[a-zA-Z0-9]{6,15}");
        return objmail.IsMatch(strpwd);
    }

    // Function to test for Character with fixed length(1-150)
    public bool IsFixedLength_Address(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex(@"^[\s\S]{0,150}$");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }

    public bool IsFixedLength_QueryContent(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex(@"^[\s\S]{0,3500}$");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }

    // Function to test for Character with fixed length(1-150)
    public bool IsFixedLength_FeedBack(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex(@"^[\s\S]{0,2000}$");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }

    // Function to test for Character with fixed length(1-15)
    public bool IsFixedLength(String strNumber)
    {
        Regex objNotWholeFixedPattern = new Regex(@"^.{0,13}$");
        return objNotWholeFixedPattern.IsMatch(strNumber);
    }


    //Function To test for AlphabetsWithSpace
    public bool IsAlphaSpace(String strToCheckAlpha)
    {
        Regex objAlphaPatternWithSpace = new Regex("^[a-zA-Z ]+$");
        return objAlphaPatternWithSpace.IsMatch(strToCheckAlpha);
    }

    //Function To test for AlphabetsWithSpace
    public bool IsAlphaNumericSpace(String strToCheckAlphaNumeric)
    {
        Regex objAlphaNumericPatternWithSpace = new Regex("^[a-zA-Z ]{4,20}+$");
        return objAlphaNumericPatternWithSpace.IsMatch(strToCheckAlphaNumeric);
    }

    //Function to test for Date
    //^((((0[13578])|(1[02]))[\/]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\/]?(([0-2][0-9])|(30)))|(02[\/]?[0-2][0-9]))[\/]?\d{4}
    public bool IsValidDate(String strToCheckDate)
    {
        Regex objDatePattern = new Regex(@"^((((0[13578])|(1[02]))[\/]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\/]?(([0-2][0-9])|(30)))|(02[\/]?[0-2][0-9]))[\/]?\d{4}");
        return objDatePattern.IsMatch(strToCheckDate);
    }

    //Function for floating point numbers with exponent
    public bool IsFloatingPointWholeNumber(String strNumber)
    {
        Regex objFloatingPointWholeNumber = new Regex(@"^\d{0,2}(\.\d{1,2})?$");
        return objFloatingPointWholeNumber.IsMatch(strNumber);
    }

    //Function for AWB number
    public bool IsAWBNumberTrue(String strNumber)
    {
        Regex objRestrictedNumber = new Regex(@"^\d{11}([a-zA-Z]{2})$");
        return objRestrictedNumber.IsMatch(strNumber);
    }

    public bool IsValidMobileNumber(String strNumber)
    {
        Regex objMobileNumber = new Regex(@"^\d{10}$");
        return objMobileNumber.IsMatch(strNumber);
    }

    public string IsCamelCase(String strNumber)
    {
        Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
        return r.Replace(strNumber, " ${x}");
    }
    //Gaurav for knowledge Centre URL

    public string ReplaceSpecialChar(String strUrl)
    {
        string url = Regex.Replace(strUrl, "[^a-zA-Z0-9]", "_");
        return url;
    }
    public string RemoveSpecialChar(String strUrl)
    {
        string url = Regex.Replace(strUrl, "[^a-zA-Z0-9' ]", " ");
        return url;
    }

    public string RemoveSpaces(String strUrl)
    {
        string url = Regex.Replace(strUrl, @"\s+", " ", RegexOptions.Multiline);
        return url;
    }



    public string RemoveSpecialChar1(string sInputString)
    {
        sInputString = sInputString.Replace("nowrap=\"nowrap\"", "").Trim();
        sInputString = sInputString.Replace("/", "").Trim();
        sInputString = sInputString.Replace("nowrap=\"nowrap\"", "").Trim();
        sInputString = sInputString.Replace(":", "").Trim();
        return sInputString;
    }
}
